﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PhysicsManager : MonoBehaviour 
{
	private Aim m_theAim;
	private Shot m_theShot;
	private PhysicsRecorder m_thePhysicsRecorder;
	private BallsPerformer m_theBallsPerformer;
	private Controller m_theController;
	private BallProperties m_theBallProperties;

	private GameObject[] m_activeBalls;
	private float m_lastAngle;

	public PhysicMaterial m_physicsMaterial;
	public Aim TheAim { get { return m_theAim; } }
	public Shot TheShot { get { return m_theShot; } }
	public PhysicsRecorder ThePhysicsRecorder { get { return m_thePhysicsRecorder; } }
	public BallsPerformer TheBallsPerformer { get { return m_theBallsPerformer; } }
	public BallProperties TheBallProperties { get { return m_theBallProperties; } }
	public Transform m_cueBallTrans;

	void Start ()
	{
		Initialize();
	}

	public void Initialize ()
	{
		m_theController = GameObject.Find("Controller").GetComponent<Controller>();
		m_theAim = new Aim();
		m_theShot = new Shot();
		m_thePhysicsRecorder = new PhysicsRecorder();
		m_theBallsPerformer = new BallsPerformer();
		m_activeBalls = GameObject.FindGameObjectsWithTag("PhysicalBall");
		m_theBallProperties = gameObject.GetComponent<BallProperties>();
		m_theBallProperties.Initialize(m_cueBallTrans.lossyScale.x * 0.5f, m_physicsMaterial);

		m_theController.OnInputAngle += CheckInputAndAim;
	}

	private void CheckInputAndAim (float angle)
	{
		if (angle != m_lastAngle)
		{
			m_theAim.ToAim(m_cueBallTrans, m_theController.GetInputDirection(angle), 0f, Vector3.zero);

			m_lastAngle = angle;
		}
	}
}
